using UnityEngine;
using System.Collections.Generic;
using System.Collections;

public class HBAO : PostEffectsBase
{
    private Camera cam;
    public Shader ssaoShader;
    private Material hbaoMaterial = null;
    public Material material{
        get {
            hbaoMaterial = CheckShaderAndCreateMaterial(ssaoShader, hbaoMaterial);
            return hbaoMaterial;
        }
    }

    [Range(0f,1f)]
    public float aoStrength = 0f;

    [Range(1, 20)]
    public int rayAngleStep = 4;
    [Range(1, 20)]
    public int rayMarchingStep = 4;

    [Range(0f,10f)]
    public float rayMarchingRadius = 1.0f;

     [Range(0f,1f)]
    public float angleBias = 0.2f;

    public Texture Nosie;

    [Range(0, 2)]
    public int DownSample = 2;

    [Range(1, 4)]
    public int BlurRadius = 2;
    [Range(0, 0.2f)]
    public float bilaterFilterStrength = 0.2f;
    public bool OnlyShowAO = false;

    public enum SSAOPassName
    {
        GenerateAO = 0,
        BilateralFilter = 1,
        Composite = 2,
    }

    protected void Start() {
		CheckResources();
        cam = this.GetComponent<Camera>();
        cam.depthTextureMode = cam.depthTextureMode | DepthTextureMode.DepthNormals;
	}

    void OnRenderImage (RenderTexture src, RenderTexture dest) {
        if (material != null) {
            int rtW = src.width >> DownSample;
            int rtH = src.height >> DownSample;

            //AO
            RenderTexture aoRT = RenderTexture.GetTemporary(rtW,rtH,0);
            hbaoMaterial.SetFloat("_AOStrength", aoStrength);
            hbaoMaterial.SetInt("_RayAngleStep", rayAngleStep);
            hbaoMaterial.SetInt("_RayMarchingStep", rayMarchingStep);
            hbaoMaterial.SetFloat("_RayMarchingRadius", rayMarchingRadius);
            hbaoMaterial.SetFloat("_AngleBias", angleBias);


            hbaoMaterial.SetTexture("_NoiseTex", Nosie);
            Graphics.Blit(src, aoRT, hbaoMaterial,(int)SSAOPassName.GenerateAO);
            //Blur
             RenderTexture blurRT = RenderTexture.GetTemporary(rtW,rtH,0);
            hbaoMaterial.SetFloat("_BilaterFilterFactor", 1.0f - bilaterFilterStrength);
            hbaoMaterial.SetVector("_BlurRadius", new Vector4(BlurRadius, 0, 0, 0));
            Graphics.Blit(aoRT, blurRT, hbaoMaterial, (int)SSAOPassName.BilateralFilter);

            hbaoMaterial.SetVector("_BlurRadius", new Vector4(0, BlurRadius, 0, 0));
            //Graphics.Blit(src, blurRT, hbaoMaterial,(int)SSAOPassName.GenerateAO);

            if (OnlyShowAO)
            {
                Graphics.Blit(blurRT, dest, hbaoMaterial, (int)SSAOPassName.BilateralFilter);
            }
            else
            {
                Graphics.Blit(blurRT, aoRT, hbaoMaterial, (int)SSAOPassName.BilateralFilter);
                hbaoMaterial.SetTexture("_AOTex", aoRT);
                Graphics.Blit(src, dest, hbaoMaterial, (int)SSAOPassName.Composite);
            }

            RenderTexture.ReleaseTemporary(aoRT);
            RenderTexture.ReleaseTemporary(blurRT);
        }
        else{
            Graphics.Blit(src, dest);
        }
    }

}
